package com.yingjia.yingjiauniversity.utils;

import java.lang.reflect.Method;

/**
 * <获取系统属性>
 *
 * @author: guoxiangxun
 * @date: 16-11-15 下午1:25
 * @version: v1.0
 */
public class SystemPropertiesUtil {
    public static final String SDK_VERSION_KEY = "ro.build.version.sdk";
    public static final String STACK_TRACE_KEY = "dalvik.vm.stack-trace-file";

    private static Class<?> mClassType = null;
    private static Method mGetMethod = null;
    private static Method mGetIntMethod = null;

    private static void init() {
        try {
            if (mClassType == null) {
                mClassType = Class.forName("android.os.SystemProperties");

                mGetMethod = mClassType.getDeclaredMethod("get", String.class);
                mGetIntMethod = mClassType.getDeclaredMethod("getInt", String.class, int.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * <getProperty, equals String SystemProperties.get(String key)>
     *
     * @param key the key
     * @return the string
     * @author: guoxiangxun
     * @date: Nov 15, 2016 1:26:30 PM
     * @version: v1.0
     */
    public static String get(String key) {
        init();

        String value = null;

        try {
            value = (String) mGetMethod.invoke(mClassType, key);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return value;
    }

    /**
     * Gets int. equals int SystemProperties.get(String key, int def)
     *
     * @param key the key
     * @param def the def
     * @return the int
     */
    public static int getInt(String key, int def) {
        init();

        int value = def;
        try {
            Integer v = (Integer) mGetIntMethod.invoke(mClassType, key, def);
            value = v.intValue();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return value;
    }

    /**
     * Gets sdk version.
     *
     * @return the sdk version
     */
    public static int getSdkVersion() {
        return getInt(SDK_VERSION_KEY, -1);
    }


    /**
     * Gets stack trace dir.
     *
     * @return the stack trace dir
     */
    public static String getStackTraceFilePath() {
        return get(STACK_TRACE_KEY);
    }


}
